﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Xml;
using Ccit.Message;
using Ccit.Message.Organ;
using Ccit.SysOrgan;
using Ccit.Utils;
using Ccit.OA.Utils;
using Newtonsoft.Json;
using Ccit.OA.MsQuestion;
using System.Drawing;
namespace Ccit.OA.Question
{
    public partial class qst_manage_usersel : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidType.Value = (Request.QueryString["type"] != "") ? Request.QueryString["type"].ToString() : "";
                _hidsRecId.Value = (Request.QueryString["qId"] != "0") ? Request.QueryString["qId"].ToString() : "";
                _hidFlag.Value = (Request.QueryString["pageFlag"] != null) ? Request.QueryString["pageFlag"].ToString() : "1";
                //_hidSelNodeId.Value = "0";
                tvUser.Attributes.Add("oncheck", "tree_oncheck(this)");
                initializePage();
              
            } 
        }
        private void initializePage()
        {
            if (_hidFlag.Value == "1")
            {
                createOrganTree();
                tvUser.ExpandLevel = 1;
            }
            if (_hidFlag.Value == "2")
            {
                createUserTree();
                tvUser.ExpandLevel = 2;
            } 
        }

        private void createUserTree()
        {
            //创建树
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createUserRootNode(ref oRoot);
            tvUser.Nodes.Add(oRoot);
            createUserSubNodes(ref oRoot);
        }

        private void createUserRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "调查范围列表"; 
            oRoot.ID = "0_0_user";
            oRoot.CheckBox = true; 
            _hidHaveChild.Value = "Y";
            _hidNodeType.Value = "user";
        }

        private void createUserSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            QuestionBLL Bll = new QuestionBLL();
            MsQuestionMes ms = Bll.GetQuestionsMessage(_hidsRecId.Value);
            string uId = SysInterface.GetUserIdsByRangeType(ms.range, ms.rangemark, ms.type);
            string[] uLen = uId.Split(',');
            string stype = _hidType.Value;
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            for (int i = 0; i < uLen.Length; i++)
            {
                string sRecId = "";
                oTreeNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oTreeNode.CheckBox = true;
                
                string sName = SysInterface.GetNameStrByIdStr(uLen[i], NameType.USER, "");
                string iNum = Bll.GetQuestionsTime(uLen[i].ToString(), _hidsRecId.Value);
                string answer = "";
                if(iNum!="")
                    answer = Server.HtmlDecode("<font color='blue'>已答</font>");
                else
                    answer = Server.HtmlDecode("<font color='red'>未答</font>");
                if (stype == "0")
                { 
                    if (iNum != "")
                    { 
                        oTreeNode.Text = sName + "(" + iNum + " "+answer+")";
                        oTreeNode.ID = uLen[i] + "_0_user";
                        if (iNum != "")
                            sRecId = uLen[i];
                        string sParam = "QId=" + _hidsRecId.Value + "&userId=" + sRecId;
                        string encodeParam = Base64Lib.Encode(sParam);
                        oTreeNode.NavigateUrl = "qst_manage_logView.aspx?" + encodeParam;
                        oTreeNode.Target = "iFrmEdit";
                        oRoot.Nodes.Add(oTreeNode); 
                    } 
                }
                else if (stype == "1")
                { 
                    if (iNum == "")
                    {
                        oTreeNode.Text = sName + "( " + answer + ")";
                        oTreeNode.ID = uLen[i] + "_0_user";
                        string sParam = "QId=" + _hidsRecId.Value + "&userId=";
                        string encodeParam = Base64Lib.Encode(sParam);
                        oTreeNode.NavigateUrl = "qst_manage_logView.aspx?" + encodeParam;
                        oTreeNode.Target = "iFrmEdit";
                        oRoot.Nodes.Add(oTreeNode);
                    }
                }
                else
                {
                   if (iNum != "")
                    {
                        oTreeNode.Text = sName + "(" + iNum + " " + answer + ")";
                        oTreeNode.ID = uLen[i] + "_0_user";
                    }
                    else
                    {
                        oTreeNode.Text = sName + "(" + answer + ")";
                        oTreeNode.ID = uLen[i] + "_0_user";
                    }
                   if (iNum != "")
                       sRecId = uLen[i];
                   string sParam = "QId=" + _hidsRecId.Value + "&userId=" + sRecId;
                   string encodeParam = Base64Lib.Encode(sParam);
                   oTreeNode.NavigateUrl = "qst_manage_logView.aspx?" + encodeParam;
                   oTreeNode.Target = "iFrmEdit";
                   oRoot.Nodes.Add(oTreeNode); 
                }
                if (iNum != "")
                {
                    if (_hidCheckUserId.Value == "")
                    {
                        oTreeNode.Checked = true;
                        _hidCheckUserId.Value = uLen[i];
                    } 
                }
            }
        }
        #region 组织机构
        private void createOrganTree()
        {
            //创建树
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createOrganRootNode(ref oRoot);
            tvUser.Nodes.Add(oRoot);
            createOrganSubNodes(ref oRoot);
        }

        //创建跟节点
        private void createOrganRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            _hidRootId.Value = login.OrganId;
            oRoot.Text = "调查范围部门列表";
            oRoot.CheckBox = true;
            oRoot.ID = "0_0_dept";
        }
        //创建子节点
        private void createOrganSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;

            string stairDeptsXML = OrganNET.GetStairDeptByOrganId(_hidRootId.Value);
            
            QuestionBLL bll = new QuestionBLL();
            MsQuestionMes ms = bll.GetQuestionsMessage(_hidsRecId.Value);
            string userId = SysInterface.GetUserIdsByRangeType(ms.range, ms.rangemark, ms.type);
            string[] uLen = userId.Split(',');
            _hidUserID.Value = userId;
            if (stairDeptsXML != "")
            {
                XmlDocument oDom = new XmlDocument();
                oDom.LoadXml(stairDeptsXML);
                if (oDom.GetElementsByTagName("DEPTS").Count > 0)
                {
                    XmlNode oNodeChild = null;
                    int childCount = Convert.ToInt32(oDom.GetElementsByTagName("DEPT").Count);
                    for (int i = 0; i < childCount; i++)
                    {
                        oNodeChild = oDom.GetElementsByTagName("DEPT").Item(i);
                        string deptId = oNodeChild["DEPTID"].InnerText;
                        string strAddrXML = bll.GetStairAddrs(userId);
                        XElement XeAddr = XElement.Parse(strAddrXML);
                        var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;
                        foreach (var eAddr in eAddrArray)
                        {
                            string deptId1 = eAddr.Element("DEPTID").Value;
                            string LAYCODE = eAddr.Element("LAYCODE").Value;
                            if (LAYCODE.Length == 4&&LAYCODE!="")
                            {
                                if (deptId == deptId1)
                                {
                                    bool bUser = bll.CheckUserbyDeptId(deptId);
                                    oTreeNode = new Microsoft.Web.UI.WebControls.TreeNode();
                                    oTreeNode.CheckBox = true;
                                    oTreeNode.Text = oNodeChild["DEPTNAME"].InnerText;
                                    oTreeNode.ID = oNodeChild["DEPTID"].InnerText + "_0_dept";
                                    if (oNodeChild.Attributes["HASCHILD"].InnerText == "1" || bUser == true) //有子节点
                                    {
                                        Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                                        oTreeNode.Nodes.Add(oSubNode);
                                    }
                                    oRoot.Nodes.Add(oTreeNode);
                                    break;
                                }
                            }
                        }   
                    }
                }
            }

            //获取部门下的人员

            List<MsUser> lstUsers = bll.GetUserByDeptId("", _hidsRecId.Value, _hidType.Value);
            foreach (MsUser u in lstUsers)
            {
                if (u.userName != "")
                {
                    oTreeNode = new Microsoft.Web.UI.WebControls.TreeNode();
                    oTreeNode.CheckBox = true;
                    oTreeNode.Text = u.userName;
                    oTreeNode.ID = u.userId + "_0_user";

                    if (u.effect == "")
                    {
                        u.userId = "";
                    }
                    else
                    { 
                        if (_hidCheckUserId.Value == "")
                        {
                             oTreeNode.Checked = true;
                            _hidCheckUserId.Value = u.userId;
                        } 
                    }
                    string sParam = "QId=" + _hidsRecId.Value + "&userId=" + u.userId;
                    string encodeParam = Base64Lib.Encode(sParam);
                    oTreeNode.NavigateUrl = "qst_manage_logView.aspx?" + encodeParam;
                    oTreeNode.Target = "iFrmEdit";
                    oRoot.Nodes.Add(oTreeNode);
                }
            }

            if (stairDeptsXML != "" || lstUsers.Count > 0)
            {
                _hidHaveChild.Value = "Y";
                _hidNodeType.Value = "dept";
            }
            else
            {
                _hidHaveChild.Value = "N";
                _hidNodeType.Value = "dept";
            }
        }
        #endregion
        /// <summary>
        /// 获组织机构取子节点
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetOrganSubNodes(string updeptId, string nodeIndex, string levelStruts,string sRecID,string type)
        {
            QuestionBLL bll = new QuestionBLL();
            bool bHaveUser = bll.CheckUserbyDeptId(updeptId);
            List<MsUser> lstUsers = bll.GetUserByDeptId(updeptId, sRecID, type);

            string subNodeXML = DeptNET.GetSubDeptsXML(updeptId);

            subNodeXML = bll.checkHasUsers(subNodeXML);
            return "dept_" + nodeIndex + "_" + subNodeXML + "_" + bHaveUser + "_" + JavaScriptConvert.SerializeObject(lstUsers) + "_" + levelStruts + "_" + updeptId;
        } 
    }
}
